import {request} from './request'
import {formatDate} from "../common/utils";

/**
 * 通过用户名查询用户
 * @param username
 * @returns {返回instance方法|发送网络请求}
 */
export const searchUserByUsername = (username) => {
  return request({
    url: '/mix/search-user',
    method: 'get',
    params: {
      username
    }
  })
};

/**
 * 添加商品
 * @param items
 * @returns {返回instance方法|发送网络请求}
 */
export const addGoods = (goods) => {
  let formData = new FormData();
  formData.append('upload_file', goods)

  // console.log('name=' + goods.name + ';describe=' + goods.describe + ';price=' + goods.price + ';store=' +
  //   goods.store + ';src=' + goods.src + ';goods_category_id=' + goods.goods_category_id + ';index=' + goods.index + ';--------------------------');
  return request({
    // header: {
    //   "Content-type": "multipart/form-data"
    // },
    url: '/mix/add-goods',
    method: 'post',
    data: {
      name: goods.name,
      describe: goods.describe,
      price: goods.price,
      store: goods.store,
      imgBase: goods.imgBase,
      goods_category_id: goods.goods_category_id,
      index: goods.index,
      date_name: formatDate(new Date(), 'yyyy0MM0dd0hh0mm0ss')
    }
  })
};
// function getDateName() { // 上传文件的日期名字
//   const dateName = formatDate(new Date(), 'yyyy0MM0dd0hh0mm0ss');
//   console.log('String date=' + dateName);
//   return dateName;
// }

/**
 * 更新商品信息
 * @param goods
 * @returns {返回instance方法|发送网络请求}
 */
export const updateGoods = (goods) => {
  return request({
    url: '/mix/update-goods',
    method: 'post',
    data: {
      id: goods.id,
      name: goods.name,
      describe: goods.describe,
      price: goods.price,
      store: goods.store,
      goods_category_id: goods.goods_category_id,
      index: goods.index,
      imgBase: goods.imgBase,
      src: goods.src,
      date_name: goods.imgBase ? formatDate(new Date(), 'yyyy0MM0dd0hh0mm0ss') : '', // 如果imgBase存在,即图片已经修改,这时,就要保证后台能够拿到date_name对修改上传的图片进行重命名
    }
  })
}

/**
 * 删除商品
 * @param id
 * @returns {返回instance方法|发送网络请求}
 */
export const deleteGoods = (id) => {
  return request({
    url: '/mix//delete-goods',
    method: 'post',
    data: {
      id
    }
  })
}

/**
 * 获取商品类目的信息
 * @param id 商品类目id
 * @param name_describe 商品类目名、描述
 * @param username 商品类目的用户名
 * @param roodCategoryId 商品类目所属的主目录
 * @param i 当前要展示的页
 * @returns {返回instance方法|发送网络请求}
 */
export const getGoodsCategory = (id, name_describe, username, roodCategoryId, i) => {
  console.log('id=' + id + ';name_describe=' + name_describe + ';username=' + username + ';root_category_id=' + roodCategoryId + ';i=' + i);
  return request({
    url: '/mix/list-goods_category',
    method: 'post',
    data: {
      id: id,
      name_describe: name_describe,
      username: username,
      root_category_id: roodCategoryId,
      i: i,
    }
  })
}

/**
 * 获取商品的信息
 * @param id 商品id
 * @param goods_category_id 商品类目id
 * @param root_category_id 商品所属的root的id
 * @param username 商家用户名
 * @param name_describe_goodsCategoryName_rootCategoryName 商品名字、描述,或者商品的商品类目名字、root类目名
 * @param i 当前要展示的页
 * @returns {返回instance方法|发送网络请求}
 */
export const getGoodsList = (id, goods_category_id, root_category_id, username, name_describe_goodsCategoryName_rootCategoryName, i) => {
  console.log('id=' + id+ ';goods_category_id=' + goods_category_id + ';root_category_id=' + root_category_id +
    ';name_describe_goodsCategoryName_username_rootCategoryName=' + name_describe_goodsCategoryName_rootCategoryName +
    ';-------------------------------------------------------');
  return request({
    url: '/mix/list-goods',
    method: 'post',
    data: {
      id,
      goods_category_id,
      root_category_id,
      username,
      name_describe_goodsCategoryName_rootCategoryName,
      i,
    }
  })
}

/**
 * 添加商品类目的网络请求
 * @param name
 * @param describe
 * @param root_category_id
 * @param user_username
 * @param index
 * @returns {返回instance方法|发送网络请求}
 */
export const setGoodsCategory = (name, describe, root_category_id, user_username, index) => {
  return request({
    url: '/mix/add-goods_category',
    method: 'post',
    data: {
      name,
      describe,
      root_category_id,
      user_username,
      index
    }
  })
}

/**
 * 修改商品类目的网络请求
 * @param goods_category
 * @returns {返回instance方法|发送网络请求}
 */
export const updateGoodsCategory = (goods_category) => {
  return request({
    url: '/mix/update-goods_category',
    method: 'post',
    data: {
      goods_category,
    }
  })
}

/**
 * 删除商品类目
 * @param id
 * @returns {返回instance方法|发送网络请求}
 */
export const deleteGoodsCategory = (id) => {
  return request({
    url: '/mix/delete-goods_category',
    method: 'post',
    data: {
      id
  }
  })
}

/**
 * 获取订单
 * @param username 用户名
 * @param code 订单编号
 * @param lessAmount 大于的总额
 * @param moreAmount 小于的总额
 * @param lessDate 大于订单日期
 * @param moreDate 小于的日期
 * @param i 要展示的页
 * @returns {返回instance方法|发送网络请求}
 */
export const getOrderList = (username, code, lessAmount, moreAmount, lessDate, moreDate, i) => {

  // 将日期格式化为后台可以识别处理的格式
  lessDate = formatDate(lessDate, "yyyy-MM-dd hh:mm:ss")
  moreDate = formatDate(moreDate, "yyyy-MM-dd hh:mm:ss")

  return request({
    url: '/mix/list-order',
    method: 'post',
    data: {
      username,
      code,
      lessAmount,
      moreAmount,
      lessDate,
      moreDate,
      i
    }
  })
}

/**
 * 获取订单明细
 * @param order_code 订单编号
 * @returns {返回instance方法|发送网络请求}
 */
export const getOrderDetail = (order_code) => {
  return request({
    url: '/mix/list-order_list',
    method: 'post',
    data: {
      order_code
    }
  })
}

/**
 * 获取交易详情
 * @param id
 * @param order_code
 * @param buy_username
 * @param bus_username
 * @param goodsCategoryId
 * @param rootCategoryId
 * @param lessPrice
 * @param morePrice
 * @param lessDate
 * @param moreDate
 * @param i
 * @param size
 * @returns {返回instance方法|发送网络请求}
 */
export const getTransaction = (id, order_code, buy_username, bus_username, goodsCategoryId, rootCategoryId, lessPrice, morePrice, lessDate, moreDate, i, size) => {

  // 将日期格式化为后台可以识别处理的格式
  lessDate = formatDate(lessDate, "yyyy-MM-dd hh:mm:ss")
  moreDate = formatDate(moreDate, "yyyy-MM-dd hh:mm:ss")

  return request({
    url: '/mix/transaction',
    method: 'post',
    data: {
      id,
      order_code,
      buy_username,
      bus_username,
      goodsCategoryId,
      rootCategoryId,
      lessPrice,
      morePrice,
      lessDate,
      moreDate,
      i,
      size,
    }
  })
}

/**
 * 添加消息的网络请求
 * @param message
 * @returns {返回instance方法|发送网络请求}
 */
export const setMessage = (message) => {
  return request({
    url: '/mix/add-message',
    method: 'post',
    data: {
      message
    }
  })
}

/**
 * 删除消息
 * @param id
 * @returns {返回instance方法|发送网络请求}
 */
export const deleteMessage = (id) => {
  return request({
    url: '/mix/delete-message',
    method: 'post',
    data: {
      id
    }
  })
}


